
/* 
 此js里面涉及到后台交互，只要实现前台的取值和显示，后台数据库请自行设计，此功能是根据fly社区模板将评论独立出来
 */
var $,fly =null;
layui.use(['fly', 'face'], function(){
	 $ = layui.$;
	 fly = layui.fly;
});
//点赞  
function zan(e){
	var othis = $(e);
	$.ajax({
	       url:"#",
	       data:{
	    	   id: othis.parents().attr("data-id")
	           ,contentid: $(".fly-admin-box").attr("data-id")
	       },
		   type:'post',
		   dataType:'json',
		   success:function(res){
			   //可这里是点赞成功数据回显的
	        if(res.status == "0"){
	          var zans = othis.find('em').html()|0;
	          $(".review-zan").attr("class","review-zan zanok");
	          zans=res.retObj.conutPraise;
	          othis.find('em').html(zans)
	          if(res.retMsg){
	        	  layer.msg(res.retMsg);
	          }
	        } 
	    }
	});
	
}
//回复
function reply(e){
	var othis = $(e);
	 var val = $('#content').val();
     var aite = '@'+ othis.parent().parent().find('.dis-detail-user cite').text().replace(/\s/g, '');
     $('#content').focus()
     if(val.indexOf(aite) !== -1) return;
     $('#content').val(aite +' ' + val);
	
}

//编辑
function edit(e){
	var othis = $(e);
	$.ajax({
	       url:"#",
	       data:{
	    	   id: othis.parents().parents().attr("data-id")
	       },
		   type:'post',
		   dataType:'json',
		   success:function(res){
			   var data=res.retObj
		       if(res.status == "0"){
		        	layer.prompt({
	        	      formType: 2
	        	      ,value: data.content
	        	      ,maxlength: 100000
	        	      ,title: '编辑评论'
	        	      ,area: ['728px', '300px']
	        	      ,success: function(layero){
	        	        fly.layEditor({
	        	          elem: layero.find('textarea')
	        	        });
	        	      }
		        	},function(value, index){
		        		$.ajax({
		        		       url:"#",
		        		       data:{
		        		    	   id: othis.parents().parents().attr("data-id")
		   	        	           ,content: value
		        		       },
		        			   type:'post',
		        			   dataType:'json',
		        			   success:function(res){
		        		        if(res.status == "0"){
		        		        	layer.close(index);
									//可以在此写查询评论回显的方法
		        		        } 
		        		    }
		        		});	
	        	    })
	        } 
	    }
	});
}


//提交平价
function subdis() {
	 var othis = $(this);
	 var content = $("#content").val();
     content = /^\{html\}/.test(content) 
       ? content.replace(/^\{html\}/, '')
     : tool.content(content);
	 
	 //存储数据库
	$.ajax({
	       url:"#",
	       data:{
	    	  contentid: $(".dis-admin-box").attr("data-id"),
	    	  content: content
	       },
		   type:'post',
		   dataType:'json',
		   success:function(res){
	        if(res.status == "0"){
	        	if(res.retMsg){
	        		layer.msg(res.retMsg);
					//可以在此写查询评论回显的方法
	        	}
	        } 
	    }
	});
	
}
//转义工具
var tool={
	escape: function(html){
      return String(html||'').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
      .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
    }

    //内容转义
    ,content: function(content){
      //支持的html标签
      var html = function(end){
        return new RegExp('\\n*\\['+ (end||'') +'(pre|hr|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*', 'g');
      };
      content = tool.escape(content||'') //XSS
      .replace(/img\[([^\s]+?)\]/g, function(img){  //转义图片
        return '<img src="' + img.replace(/(^img\[)|(\]$)/g, '') + '">';
      }).replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;" class="dis-aite">$1</a>$2') //转义@
      .replace(/face\[([^\s\[\]]+?)\]/g, function(face){  //转义表情
        var alt = face.replace(/^face/g, '');
        return '<img alt="'+ alt +'" title="'+ alt +'" src="' + fly.faces[alt] + '">';
      }).replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function(str){ //转义链接
        var href = (str.match(/a\(([\s\S]+?)\)\[/)||[])[1];
        var text = (str.match(/\)\[([\s\S]*?)\]/)||[])[1];
        if(!href) return str;
        var rel =  /^(http(s)*:\/\/)\b(?!(\w+\.)*(sentsin.com|layui.com))\b/.test(href.replace(/\s/g, ''));
        return '<a href="'+ href +'" target="_blank"'+ (rel ? ' rel="nofollow"' : '') +'>'+ (text||href) +'</a>';
      }).replace(html(), '\<$1 $2\>').replace(html('/'), '\</$1\>') //转移HTML代码
      .replace(/\n/g, '<br>') //转义换行   
      return content;
    }
}
